Ext.define('cloud.controller.Call', {
    extend: 'Ext.app.Controller',
    config: {

        refs: {
            call: 'call',
            calladd: 'calladd',
            calledit: 'calledit',
            call_search_1: 'call #call_search_1',
            call_btn_add: 'call #call_btn_add',
            call_upload: 'calladd #call_upload',
            call_edit_upload: 'calledit #call_edit_upload',
            call_submit: 'calladd #call_submit',
            call_edit: 'calledit #calledit_edit',
            call_del: 'calledit #calledit_del',
            callSelect: 'call #callSelect',
            call_image_del: 'calledit dataview'
        },
        control: {
            /**
             * search call
             */
            call_search_1: {
                change: function () {
                    var Store = Ext.getStore('CallDistrictFindStore');
                    var value = this.getCall_search_1().getValue();
                    Store.clearFilter();
                    if (value != "") {
                        Store.filter('name', value, true);
                    }
                    Store.load({params: {cadid: localStorage.getItem('cadid')}});
                }
            },

            /**
             * show calladd page
             */
            call_btn_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var calladd = this.getCalladd()||Ext.create('cloud.view.call.callAdd');
                    this.getCall().push(calladd);
                    Ext.Viewport.unmask();
                }
            },
            /**
             * show callList
             */
            callSelect: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    Ext.getStore('CallDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                    var calledit = this.getCalledit()||Ext.create('cloud.view.call.callEdit');
                    calledit.setRecord(record);
                    this.getCall().push(calledit);
                    Ext.Viewport.unmask();
                }
            },
            /**
             * del call_image
             */
            call_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getCalledit().submit({
                                url: GLOBAL.api_url + 'call_delete',
                                scope: this,
                                method: 'POST',
                                params: {
                                    //传递参数
                                    cdcid: Ext.getCmp('calledit_cdcid').getValue()
                                },
                                success: this.exesucess
                            });
                        }
                    }, this);
                }
            },
            call_image_del: {
                itemtap: function (dw, index, record) {


                }
            },

            /**
             * submit call
             */
            call_submit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('calladd_name').getValue()))
                        return Ext.Msg.alert('请输入名称');
                    if (Ext.isEmpty(Ext.getCmp('calladd_tel').getValue()))
                        return Ext.Msg.alert('请输入电话号码');
                    if (Ext.isEmpty(Ext.getCmp('calladd_address').getValue()))
                        return Ext.Msg.alert('请输入呼叫说明');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getCalladd().submit({
                        url: GLOBAL.api_url + 'call_add',

                        scope: this,
                        method: 'POST',
                        params: {
                            //传递参数
                            cadid: localStorage.getItem('cadid')
                            //name:Ext.getCmp('call_name').getValue(),

                        },
                        success: this.exesucess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * edit call
             */
            call_edit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('calledit_name').getValue()))
                        return Ext.Msg.alert('请输入名称');
                    if (Ext.isEmpty(Ext.getCmp('calledit_tel').getValue()))
                        return Ext.Msg.alert('请输入电话号码');
                    if (Ext.isEmpty(Ext.getCmp('calledit_address').getValue()))
                        return Ext.Msg.alert('请输入说明');
                    Ext.Viewport.mask({xtype: 'loadmask'});

                    this.getCalledit().submit({
                        url: GLOBAL.api_url + 'call_update',
                        method: 'POST',
                        scope: this,
                        success: this.exesucess
                    });
                    Ext.Viewport.unmask();
                }
            }
        }
    },
    exesucess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('CallDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
            this.getCall().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
})
;
